package business_logic.view;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Graphics2D;
import business_logic.HighScores;
import fiuba.algo3.titiritero.dibujables.SuperficiePanel;
import fiuba.algo3.titiritero.modelo.SuperficieDeDibujo;

public class HighScoresView implements GameView
{

    private static final int SCOREBOARD_WIDTH = 400;
    private static final int SCOREBOARD_HEIGHT = 300;
    boolean m_enabled = false;
    private HighScores m_highScores;
    
    public HighScoresView( HighScores highScores )
    {
        m_highScores = highScores;
    }
    
    public void setEnabled( boolean enabled )
    {
        m_enabled = enabled;
    }
    
    private void drawTitle( SuperficieDeDibujo graphicSurface )
    {
        /* Dibujo el título */
        TextView scoreText = new TextView( "HighScores" );
        scoreText.setColor(Color.RED);
        scoreText.setSize(20);  // Revisar
        scoreText.setPosition( 200, 90 );    // Revisar!
        scoreText.centerHorizontally();
        scoreText.dibujar(graphicSurface);
    }
    
    private void drawScoreBoard(SuperficieDeDibujo graphicSurface)
    {
        
        SuperficiePanel panel = (SuperficiePanel)graphicSurface;
        Graphics2D g2 = (Graphics2D)panel.getBuffer();
        g2.setColor(Color.DARK_GRAY);
        
        // Centrar y dibujar.
        int panelWidth  = (int) panel.getWidth();
        int panelHeight = (int) panel.getHeight();
        
        Composite c = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.8f);
        g2.setComposite(c);
        
        g2.fillRect( (panelWidth-SCOREBOARD_WIDTH)/2,
                     (panelHeight-SCOREBOARD_HEIGHT)/2, 
                      SCOREBOARD_WIDTH,
                      SCOREBOARD_HEIGHT
                   );
    }
    
    @Override
    public void dibujar(SuperficieDeDibujo graphicSurface)
    {
        if ( m_enabled )
        {
            this.drawScoreBoard(graphicSurface);
            this.drawTitle(graphicSurface);
            
            /* Dibujo los Highscores */
            int scoreIndex = 0;
            for ( Integer score : m_highScores.getScores() )
            {
                scoreIndex++;

                String scoreString = String.format("%02d. %010d", scoreIndex, score);
                
                TextView scoreText = new TextView( scoreString );
                scoreText.setColor(Color.WHITE);
                scoreText.setSize(20);
                scoreText.setPosition( 200, 120+25*(scoreIndex-1) );
                scoreText.centerHorizontally();
                scoreText.dibujar(graphicSurface);
            }
        }
    }

    @Override
    public boolean isDone()
    {
        return false;
    }

    @Override
    public int getZIndex()
    {
        return 100000;
    }

}
